﻿namespace ChinaUnicom.Tasks
{
    using Avatar.Schedule;
    using ChinaUnicom.Models;
    using System;
    using System.Net;
    using System.Linq;

    public class PackageTask : ITask
    {
        public Package Package { get; private set; }

        public PackageTask()
        {
            Package = new Package();
            Package.Occurred = DateTime.Now;
        }

        public void Start()
        {
            if (Started != null)
            {
                Started(this, EventArgs.Empty);
            }

            Factory.Operation.RequestPackageInfo(x =>
            {
                try
                {
                    if (x.Status == WebExceptionStatus.Success)
                    {
                        var items = Factory.Parser.Parse(x.Result, ItemType.Package);
                        PackageInfoHandle(items);

                        Context.Current.Account.LastUpdated = DateTime.Now; 

                        if (Completed != null)
                        {
                            Completed(this, EventArgs.Empty);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (Failed != null)
                    {
                        Failed(this, new FailedEventArgs(this, ex));
                    }
                }
            });
        }

        void PackageInfoHandle(ItemCollection items)
        {
            /*
             * 客户姓名
             * 手机号码
             * 套餐名称
             * 查询日期
             * 起止日期
             */

            // 修改顺序
            ItemCollection tempItems = new ItemCollection();

            tempItems.Add(items.SingleOrDefault(x => x.Name.Contains("语音电话")));
            tempItems.Add(items.SingleOrDefault(x => x.Name.Contains("上网流量")));

            foreach (var item in items)
            {
                if (!tempItems.Contains(item))
                {
                    tempItems.Add(item);
                }
            }

            foreach (var item in tempItems)
            {
                switch (item.Name)
                {
                    case "套餐名称":
                        Package.Name = item.Value;
                        break;
                    case "查询日期":
                        break;
                    case "起止日期":
                        string[] times = item.Value.Split(new[] { '~' });
                        if (times.Length == 2)
                        {
                            DateTime started, ended;
                            if (DateTime.TryParse(times[0], out started))
                            {
                                Package.Started = started;
                            }
                            if (DateTime.TryParse(times[1], out ended))
                            {
                                Package.Ended = ended;
                            }
                        }
                        break;
                    default:
                        if (item.HasDetails)
                        {
                            Package.Items.Add(item);
                        }
                        break;
                }
            }
        }

        public int Delay { get { return 50; } }

        public bool IsUnique { get { return true; } }

        public event EventHandler Completed;

        public event EventHandler Started;

        public event EventHandler<FailedEventArgs> Failed;
    }
}
